﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MoneyLover.WCFMoneyLover;

namespace MoneyLover.Pages
{
    public partial class Thu : PhoneApplicationPage
    {
        public Thu()
        {
            InitializeComponent();
        }
        THU_CHI thuchi = new THU_CHI();
        MoneyLoverServiceClient _db = new MoneyLoverServiceClient();
        private void txttheloai_MouseEnter(object sender, MouseEventArgs e)
        {
            if (lstlistoaithu.Visibility == Visibility.Collapsed)
            {
                lstlistoaithu.Visibility = Visibility.Visible;
                
            }
            else
            {
                lstlistoaithu.Visibility = Visibility.Collapsed;
            }
        }

        private void txtkehoach_MouseEnter(object sender, MouseEventArgs e)
        {
            if (lstkehoach.Visibility == Visibility.Collapsed)
            {
                lstkehoach.Visibility = Visibility.Visible;
            }
            else
            {
                lstkehoach.Visibility = Visibility.Collapsed;
            }
        }
        protected void Loadreference()
        {
            txtngaythu.Text = DateTime.Now.ToShortDateString();
            _db.GetListLoaiThuCompleted +=_db_GetListLoaiThuCompleted;
            _db.GetListLoaiThuAsync();
            _db.GetListKeHoachCompleted += _db_GetListKeHoachCompleted;
            _db.GetListKeHoachAsync();
            lstkehoach.Visibility = Visibility.Collapsed;
            lstlistoaithu.Visibility = Visibility.Collapsed;
        }

        void _db_GetListKeHoachCompleted(object sender, GetListKeHoachCompletedEventArgs e)
        {
            if (e.Result!=null)
            {
                lstkehoach.ItemsSource = e.Result;
            }
        }

        void _db_GetListLoaiThuCompleted(object sender, GetListLoaiThuCompletedEventArgs e)
        {
            if (e.Result!=null)
            {
                lstlistoaithu.ItemsSource = e.Result;
            }
        }

       
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Loadreference();
        }

        private void lstlistoaithu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstlistoaithu.Visibility = Visibility.Collapsed;
            var objdb = (LOAIGIAODICH)lstlistoaithu.SelectedItem;
            txttheloai.Text = objdb.TENLOAIGD;
            thuchi.MALOAIGD = objdb.ID;
        }

        private void lstkehoach_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstkehoach.Visibility = Visibility.Collapsed;
            var objdb = (KEHOACH)lstkehoach.SelectedItem;
            txtkehoach.Text = objdb.TENKH;
            thuchi.MAKH = objdb.ID;
        }

        private void btnsave_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(Invalid()))
            {
                thuchi.TENGD = txttenkhoanthu.Text.Trim();
                thuchi.SOTIEN = decimal.Parse(txtsotien.Text.Trim());
                thuchi.GHICHU = txtghichu.Text.Trim();
                thuchi.LAPLAI = false;
                String mathu=NavigationContext.QueryString["id"];
                if(!String.IsNullOrEmpty(mathu))
                {
                     int magd = int.Parse(mathu);
                    thuchi.MAGD=magd;
                }
                thuchi.USERNAME = Contanst._tkdangnhap.USERNAME;
                _db.InsertThuChiCompleted += _db_InsertThuChiCompleted;
                _db.InsertThuChiAsync(thuchi);
            }
            else
            {
                MessageBox.Show(Invalid());
            }
           

        }

        void _db_InsertThuChiCompleted(object sender, InsertThuChiCompletedEventArgs e)
        {
           if (e.Result==false)
           {
               MessageBox.Show("Thêm khoản thu không thành công");
           }
           else
           {
               NavigationService.Navigate(new Uri("/Pages/SoChiTieu.xaml", UriKind.Relative));
           }
        }
        private string Invalid()
        {
            String error = "";
            if (String.IsNullOrEmpty(txttenkhoanthu.Text.Trim()))
            {
                error += "\n Tên khoản thu không được để trống";
            }
            try
            {
                float a = float.Parse(txtsotien.Text.Trim());
            }
            catch 
            {
                error += "\n số tiền không hợp lệ";
            }
            if (String.IsNullOrEmpty(txtngaythu.Text.Trim()))
            {
                error += "\nChưa nhập ngày thu";
            }
            else
            {
                string words = txtngaythu.Text.Trim();
                try
                {
                    string[] split = words.Split(new Char[] { '/' });
                    int ngay = int.Parse(split[0]);
                    int Thang = int.Parse(split[1]);
                    int nam = int.Parse(split[2]);
                    thuchi.NGAYGD = new DateTime(nam, Thang, ngay);
                }
                catch
                {
                    error += "\nNgày vay không hợp lệ";
                }


            }
            return error;

        }
        private void bntsaveandadd_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(Invalid()))
            {
                thuchi.TENGD = txttenkhoanthu.Text.Trim();
                thuchi.SOTIEN = decimal.Parse(txtsotien.Text.Trim());
                thuchi.GHICHU = txtghichu.Text.Trim();
                thuchi.NGAYGD = DateTime.Now;
                thuchi.LAPLAI = false;
                String mathu = NavigationContext.QueryString["id"];
                if (!String.IsNullOrEmpty(mathu))
                {
                    int magd = int.Parse(mathu);
                    thuchi.MAGD = magd;
                }
                thuchi.USERNAME = Contanst._tkdangnhap.USERNAME;
                _db.InsertThuChiCompleted += _db_InsertThuChiCompletedadd;
                _db.InsertThuChiAsync(thuchi);
            }
            else
            {
                MessageBox.Show(Invalid());
            }
        }
        void _db_InsertThuChiCompletedadd(object sender, InsertThuChiCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                MessageBox.Show("Thêm khoản thu không thành công");
            }
            else
            {
                NavigationService.Navigate(new Uri("/Pages/Giaodich.xaml", UriKind.Relative));
            }
        }

        private void txtngaythu_MouseEnter(object sender, MouseEventArgs e)
        {
            txtngaythu.Text = "";
        }

        private void txtngaythu_MouseLeave(object sender, MouseEventArgs e)
        {
            if (String.IsNullOrEmpty(txtngaythu.Text.Trim()))
            {
                txtngaythu.Text = DateTime.Now.ToShortDateString();
            }
        }
    }
}